package com.ouheng.shop.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ouheng.common.domain.Tree;
import com.ouheng.common.utils.BuildTree;
import com.ouheng.shop.dao.ProductDao;
import com.ouheng.shop.dao.ProductGroupDao;
import com.ouheng.shop.dao.ProductProductGroupDao;
import com.ouheng.shop.domain.ProductDO;
import com.ouheng.shop.domain.ProductGroupDO;
import com.ouheng.shop.domain.ProductProductGroupDO;
import com.ouheng.shop.domain.ProductRelevanceDO;
import com.ouheng.shop.service.ProductGroupService;



@Service
public class ProductGroupServiceImpl implements ProductGroupService {
	@Autowired
	private ProductGroupDao productGroupDao;
	
	@Autowired
	private ProductDao productDao;
	
	@Autowired
	private ProductProductGroupDao productProductGroupDao;
	
	@Override
	public ProductGroupDO get(Long id){
		return productGroupDao.get(id);
	}
	
	@Override
	public List<ProductGroupDO> list(Map<String, Object> map){
		return productGroupDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return productGroupDao.count(map);
	}
	
	@Override
	@Transactional
	public int save(ProductGroupDO productGroup){
		return productGroupDao.save(productGroup);
	}
	
	@Override
	@Transactional
	public int update(ProductGroupDO productGroup){
		return productGroupDao.update(productGroup);
	}
	
	@Override
	@Transactional
	public int remove(Long id){
		return productGroupDao.remove(id);
	}
	
	@Override
	@Transactional
	public int batchRemove(Long[] ids){
		return productGroupDao.batchRemove(ids);
	}

	@Override
	public Tree<ProductGroupDO> getTree() {
		List<Tree<ProductGroupDO>> trees = new ArrayList<Tree<ProductGroupDO>>();
		List<ProductGroupDO> productGroupDOs = productGroupDao.list(new HashMap<String,Object>(16));
		for (ProductGroupDO productGroupDO : productGroupDOs) {
			Tree<ProductGroupDO> tree = new Tree<ProductGroupDO>();
			tree.setId(productGroupDO.getId().toString());
			tree.setParentId(productGroupDO.getParentId().toString());
			tree.setText(productGroupDO.getName());
			Map<String, Object> state = new HashMap<>(16);
			state.put("opened", true);
			tree.setState(state);
			trees.add(tree);
		}
		// 默认顶级菜单为０，根据数据库实际情况调整
		Tree<ProductGroupDO> t = BuildTree.build(trees);
		return t;
	}

	@Override
	public boolean checkGroupHasProduct(Long proId) {
		//查询部门以及此部门的下级部门
		int result = productGroupDao.getGroupProduct(proId);
		return result==0?true:false;
	}

	@Override
	public Tree<ProductDO> getTree(Long id) {
		List<ProductDO> ids = productDao.list(new HashMap<String, Object>(16));
		List<Long> ppids = productProductGroupDao.listProductidByGroupid(id);
		List<Long> temp = ppids;
//		for (ProductDO regionDO : ids) {
//			if (temp.contains(regionDO.getId())) {
//				ppids.remove(regionDO.getId());
//			}
//		}
		
		List<Tree<ProductDO>> trees = new ArrayList<Tree<ProductDO>>();
		List<ProductDO> regionDOs = productDao.list(new HashMap<String, Object>(16));
		for (ProductDO regionDO : regionDOs) {
			Tree<ProductDO> tree = new Tree<ProductDO>();
			tree.setId(regionDO.getId().toString());
			tree.setParentId(null);
			tree.setText(regionDO.getName());
			Map<String, Object> state = new HashMap<>(16);
			Long menuId = regionDO.getId();
			if (ppids.contains(menuId)) {
				state.put("selected", true);
			} else {
				state.put("selected", false);
			}
			tree.setState(state);
			trees.add(tree);
		}
		// 默认顶级菜单为０，根据数据库实际情况调整
		Tree<ProductDO> t = BuildTree.build(trees);
		return t;
	}

	@Override
	@Transactional
	public void saveRelevance(ProductGroupDO productGroup) {
		productProductGroupDao.removeByGroupid(productGroup.getId());
		List<Long> ppids = productGroup.getPpids();
		for (Long long1 : ppids) {
			
			ProductProductGroupDO productProductGroupDO=new ProductProductGroupDO();
			productProductGroupDO.setGroupid(productGroup.getId());
			productProductGroupDO.setProductid(long1);
			productProductGroupDao.save(productProductGroupDO);
		}
	}

	@Override
	@Transactional
	public void saveRelevance(Long pid, Long[] ppids) {
		productProductGroupDao.removeByGroupid(pid);
		if(pid!=null&&ppids.length>0){
			for (Long string : ppids) {
				ProductProductGroupDO productProductGroupDO = new ProductProductGroupDO();
				productProductGroupDO.setGroupid(pid);
				productProductGroupDO.setProductid(string);
				productProductGroupDao.save(productProductGroupDO);
			}
		}
	}
	
}
